package com.danskin.wechat.api.client;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

import com.danskin.wechat.util.PropertyUtil;

public class DanskinSMSApiClient extends ApiClient {

	private static final String SMS_API_URL = PropertyUtil.get("sms_api_url");
	private static final String SMS_API_USERID = PropertyUtil.get("sms_api_userid");
	private static final String SMS_API_PASSWORD = PropertyUtil.get("sms_api_password");

	private volatile static DanskinSMSApiClient singleton;

	private DanskinSMSApiClient() {
	}

	public static DanskinSMSApiClient createInstance() {
		if (singleton == null) {
			synchronized (DanskinSMSApiClient.class) {
				if (singleton == null) {
					singleton = new DanskinSMSApiClient();
				}
			}
		}
		return singleton;
	}

	public void sendMessage(String mobile, String msg) {
		String multixmt = getmultixmt(mobile, msg);
		String paramStr = "userId=" + SMS_API_USERID + "&password=" + SMS_API_PASSWORD + "&multixmt=" + multixmt;
				 

		sendGet2(SMS_API_URL, paramStr);
	}

	public String getmultixmt(String phone, String msg) {

		String urlsafe_base64encode = urlsafe_b64encode(msg);
		String return_info = "*|" + phone + "|" + urlsafe_base64encode + "|SvrType|P1|P2|P3|P4|0|01234|0|0|1";
		return return_info;
	}

	public String urlsafe_b64encode(String inputData) {
		if (null == inputData) {
			return null;
		}
		return new String(Base64.encodeBase64(to_gbk(inputData)));

	}

	public byte[] to_gbk(String msg) {

		try {
			return msg.getBytes("GBK");
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		return null;
	}
}
